package com.application.lucky.request;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.application.lucky.bean.UserBean;
import com.application.lucky.bean.jsonBean.BankItemBean;
import com.application.lucky.bean.jsonBean.NoticeMessageBean;
import com.application.lucky.bean.sqliteBean.BankBean;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.request.nav.UrlList;
import com.application.lucky.request.nav.UrlManager;
import com.application.lucky.utils.Flog;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;
import com.baselib.libnetwork.net.libnetworkcomponent.PieBaseRequest;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.application.lucky.request.nav.UrlList.user_pic_updateHp;
import static com.application.lucky.request.nav.UrlList.user_self;

public class UserRequest extends PieBaseRequest {
    private static final String TAG = "UserRequest";

    public void getSelfData(CallBackEx httpCallback) {
        String url = UrlManager.getUrl(user_self);
        get(url, httpCallback);
    }

    public void user_search(String s, CallBackEx<BaseResponse<UserBean>> callback) {
        String url = UrlManager.getUrl(UrlList.user_search);
        get(url, "nick_name", s, callback);
    }

    public void user_info(String s, CallBackEx<BaseResponse<UserBean>> callback) {
        String url = UrlManager.getUrl(UrlList.user_info);
        get(url, "user_id", s, callback);
    }

    public void user_init_attr(String invite, final CallBackEx<BaseResponse<UserBean>> ex) {
        String url = UrlManager.getUrl(UrlList.user_init_attr);
        JSONObject object = new JSONObject();

        object.put("2", "1");//性别
        object.put("1", UserManagers.getInstance().getLoginName());//昵称
//        object.put("100", "很懒，什么都没有留下");//个人签名
//        object.put("4",System.currentTimeMillis()+"");//生日
//        object.put("101","城市");//
        String s = object.toJSONString();
        Map<String, String> param = new HashMap<>();
        param.put("user_attr", s);
        if (!TextUtils.isEmpty(invite)) {
            param.put("code", invite);
        }
        post(url, param, new CallBackEx<BaseResponse<UserBean>>() {
            @Override
            public void onFailed(int code) {
                if (ex != null) {
                    ex.onFailed(code);
                }
            }

            @Override
            public void onSuccess(BaseResponse<UserBean> userBeanBaseResponse) {
                if (ex != null) {
                    ex.onSuccess(userBeanBaseResponse);
                }
            }

        });
    }

    public void user_init_attr(BankBean bankBean, String phoneNum, final CallBackEx<BaseResponse<UserBean>> ex) {

    }

    public void user_update_attr(String realName, String phoneNum, final CallBackEx<BaseResponse<UserBean>> ex) {
        String url = UrlManager.getUrl(UrlList.user_update_attr);
        JSONObject object = new JSONObject();

//        object.put("153", bankBean.name);//名字
//        object.put("154", bankBean.num);//卡号
//        object.put("155", bankBean.accountNamw);//银行名字
//        object.put("156", bankBean.accountAddress);//银行地址 

        object.put("18", realName);//真实的名字
        object.put("99", phoneNum);
        Flog.d(TAG, "user_update_attr 18:" + realName + " 99:" + phoneNum);
        String s = object.toJSONString();
        Map<String, String> param = new HashMap<>();
        param.put("user_attr", s);
        post(url, param, new CallBackEx<BaseResponse<UserBean>>() {
            @Override
            public void onFailed(int code) {
                if (ex != null) {
                    ex.onFailed(code);
                }
            }

            @Override
            public void onSuccess(BaseResponse<UserBean> userBeanBaseResponse) {
                if (ex != null) {
                    ex.onSuccess(userBeanBaseResponse);
                }
            }

        });
    }

    public void user_update_attr(String nick, String birthday, String city, final CallBackEx<BaseResponse<UserBean>> ex) {
        String url = UrlManager.getUrl(UrlList.user_update_attr);
        JSONObject object = new JSONObject();

        if (!TextUtils.isEmpty(nick)) {
//            object.put("1", nick);//昵称
            object.put("12", nick);//showname
        }
        if (!TextUtils.isEmpty(birthday)) {
            object.put("4", birthday);
        }
        if (!TextUtils.isEmpty(city)) {
            object.put("101", city);
        }
        String s = object.toJSONString();
        Map<String, String> param = new HashMap<>();
        param.put("user_attr", s);
        post(url, param, ex);
    }

    public void updateHeadPic(String path, final CallBackEx<BaseResponse<String>> ex) {
        String url = UrlManager.getUrl(user_pic_updateHp);
        Flog.d("updateHeadPic", "updateHeadPic url:" + url);
        Map<String, Object> map = new HashMap();
        map.put("user_head_pic", new File(path));
        postFile(url, map, ex, null, new File(path));
    }

    public void getNotice(CallBackEx<NoticeMessageBean> callback) {
        String url = UrlManager.getUrl(UrlList.periphery_get_notice);
        Flog.d(TAG, "getNotice url:" + url);
        get(url, callback);
    }

    /**
     * 添加card不需要
     *
     * @param bank
     * @param bankAddress
     * @param bankName
     * @param userName
     * @param id
     * @param callBackEx
     */
    public void bindCard(String bank, String bankAddress, String bankName, String userName, String id, final CallBackEx<BaseResponse> callBackEx) {
        String url = UrlManager.getUrl(UrlList.bank_add);
        Flog.d(TAG, "bindCard url:" + url);
        JSONObject object = new JSONObject();
        object.put("bank", bank);
        object.put("bankAddress", bankAddress);
        object.put("bankName", bankName);
        object.put("name", userName);

        Map<String, String> param = new HashMap<>();
        param.put("bankInfoBean", object.toJSONString());
        postBodyJson(true, url, param, object.toJSONString(), callBackEx);
    }

    public void updateCard(String bankId, String bank, String bankAddress, String bankName, String userName, String id, final CallBackEx<BaseResponse> callBackEx) {
        String url = UrlManager.getUrl(UrlList.bank_update);
        Flog.d(TAG, "updateCard url:" + url);
        JSONObject object = new JSONObject();
        object.put("id", bankId);
        object.put("bank", bank);
        object.put("bankAddress", bankAddress);
        object.put("bankName", bankName);
        object.put("name", userName);

        Map<String, String> param = new HashMap<>();
        param.put("bankInfoBean", object.toJSONString());
        postBodyJson(true, url, param, object.toJSONString(), callBackEx);
    }

    public void getAlreadyBindBank(CallBackEx<BaseResponse<List<BankItemBean>>> callback) {
        String url = UrlManager.getUrl(UrlList.bank_list);
        Flog.d(TAG, "getAlreadyBindBank url:" + url);
        get(url, callback);
    }

    public void unBindCard(String bandId, CallBackEx<BaseResponse> callback) {
        String url = UrlManager.getUrl(UrlList.bank_remove);
        Flog.d(TAG, "unBindCard url:" + url);
        Map<String, String> param = new HashMap<>();
        param.put("bank_id", bandId);
        post(url, param, callback);
    }

    public void relation_partner_list(CallBackEx<BaseResponse> callback) {
        String url = UrlManager.getUrl(UrlList.relation_partner_list);
        Flog.d(TAG, "relation_partner_list url:" + url);
        get(url, callback);
    }

}
